package se.jayway.millionaire.internal.game;

import se.jayway.millionaire.model.game.Answer;
import se.jayway.millionaire.model.game.Game;
import se.jayway.millionaire.model.game.GameSession;
import se.jayway.millionaire.model.game.Player;
import se.jayway.millionaire.model.game.PlayerChoice;

public class SimpleGameSession implements GameSession {

	private final Game game;
	private final Player player;
	private int correctAnswersCount;
	private int wrongAnswersCount;
	private int correctAnswersInRowCount;
	private int noAnswersCount;

	public SimpleGameSession(Game game, Player player) {
		this.game = game;
		this.player = player;
	}

	public Player getPlayer() {
		return player;
	}
	
	public int countCorrectAnswers() {
		return correctAnswersCount;
	}

	public int countWrongAnswers() {
		return wrongAnswersCount;
	}

	public int countCorrectAnswersInRow() {
		return correctAnswersInRowCount;
	}

	public int countNoAnswers() {
		return noAnswersCount;
	}
	
	// TODO How to make sure this method is only called once and only once per PlayerChoice?
	protected void updateScoreCard(PlayerChoice gameRound, Answer correctAnswer) {
		Answer playerAnswer = gameRound.getPlayerAnswer();
		if (playerAnswer == null) {
			noAnswersCount++;
			correctAnswersInRowCount = 0;
		} else if (playerAnswer == correctAnswer) {
			correctAnswersCount++;
			correctAnswersInRowCount++;
		} else {
			wrongAnswersCount++;
			correctAnswersInRowCount = 0;
		}
	}

	public void end() {
		game.endGameSession(this);
	}
}
